<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div style="width: 100%">
    <div style=" width: 100%;height: 100%;">
      <BasicForm style="margin-top:8px;" @register="registerForm" />
    </div>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'project',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, watch } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { FormProps } from '/@/components/Form';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { companyTreeData } from '/@/api/sys/company';
  import { switchCorp, getCorpCache } from '/@/api/common/common';

  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { showMessage } = useMessage();
  const [registerForm, { resetFields, validate, setFieldsValue, getFieldsValue }] = useForm({
    labelWidth: 80,
    schemas: [
      {
        // label: t(''),
        field: 'code',
        fieldLabel: 'name',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          params: {
            // isShowCode: true,
          },
          canSelectParent: false,
          placeholder: '请选择公司',
          allowClear: true,
          immediate: true,
          onChange: async (e) => {
            // 清除时调用
            if (!e) {
              await switchCorp({ companyCode: '' });
              await userStore.setProjecte(getFieldsValue());
            }
          },
          onSelect: async (value, node, extra) => {
            console.log(value);
            await switchCorp({
              companyCode: value ? value : '',
            });
            await userStore.setProjecte({
              code:node.id,
              name:node.name,
            });
          },
        },
        itemProps: { validateTrigger: 'blur' },
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });
  watch(
    () => userStore.getProjecte,
    (val, prevVal) => {
      let obj = userStore.getProjecte;
      setFieldsValue({
        // name: `(${obj.code})${obj.name}`,
        name: obj.name,
        code: obj.code,
      });
    },
    // {
    //   immediate: true,
    // },
  );

  onMounted(async () => {
    // console.log(userStore.project,'====');
    // console.log(userStore.userInfo,'userInfo');
    let res = await getCorpCache();
    await userStore.setProjecte({
      code: res.data.companyCode,
      name: res.data.companyName,
    });
    let obj = userStore.getProjecte;
    setFieldsValue({
      // name: `(${obj.code})${obj.name}`,
      name: obj.name,
      code: obj.code,
    });
  });
</script>
